package com.qbccn.onlinetest.controller;

import com.qbccn.onlinetest.domain.OtUser;
import com.qbccn.onlinetest.dto.ResponseResult;
import com.qbccn.onlinetest.service.CompanyService;
import com.qbccn.onlinetest.service.SystemService;
import com.qbccn.onlinetest.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author linke
 * @date 2020/7/4 16:34
 * @description
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private SystemService systemService;
  @Autowired
  private CompanyService companyService;
  @Autowired
  private UserService userService;

  @GetMapping("/index")
  public String index() {
    request.setAttribute("styleType", systemService.getStyleSet());
    return "admin/index";
  }

  @GetMapping("/welcome")
  public String welcome() {
    return "admin/welcome";
  }

  @GetMapping("/setSystemStyle")
  public String setSystemStyle() {
    request.setAttribute("getStyles", systemService.getStyles());
    return "admin/setSystemStyle";
  }

  @PostMapping("/updateSystemStyle")
  @ResponseBody
  public ResponseResult updateSystemStyle(Integer id) {
    return systemService.updateSystemStyle(id);
  }

  @GetMapping("/userFind")
  public String userFind() {
    return "admin/userFind";
  }

  @GetMapping("/userPower")
  public String userPower() {
    return "admin/userPower";
  }

  @GetMapping("userAdmin")
  public String userAdmin() {
    return "admin/userAdmin";
  }

  @GetMapping("addUserPage")
  public String addUserPage() {
    return "/admin/addUserPage";
  }

  @GetMapping("dataPowerAdmin")
  public String dataPowerAdmin() {
    return "admin/dataPowerAdmin";
  }

  @GetMapping("menuPowerAdmin")
  public String menuPowerAdmin() {
    return "admin/menuPowerAdmin";
  }

  @GetMapping("companyService/{username}")
  @ResponseBody
  public ResponseResult companyService(@PathVariable("username") String username) {
    OtUser user = userService.getUserInfo(username);
    if (user == null) {
      return new ResponseResult(1, "用户不存在");
    }
    return new ResponseResult(0,
        companyService.getCompanyAll(username));
  }

  @RequestMapping("getMyDataPermission")
  @ResponseBody
  public ResponseResult getMyDataPermission() {
    return new ResponseResult(0, companyService
        .getMyDataPermission(request.getSession().getAttribute("username").toString()));
  }

  @PostMapping("updateDataPermission")
  @ResponseBody
  public ResponseResult updateDataPermission(@RequestParam("username") String username,
      @RequestParam("list") List<String> list) {
    if (StringUtils.isEmpty(username) || list.isEmpty()) {
      new ResponseResult(1, "入参不能为空！");
    }
    companyService.updateDataPermission(username, list);
    return new ResponseResult(0, "提交成功！");
  }
}

